package com.alibaba.incubator.loadtestermini.model.behavior.organism;

import java.util.List;

import org.springframework.beans.factory.InitializingBean;

import com.alibaba.incubator.loadtestermini.model.behavior.SharedResourceHolder;
import com.alibaba.incubator.loadtestermini.model.load.ThinkingTime;
import com.alibaba.incubator.loadtestermini.model.measurement.recorder.SceneRecorder;
import com.alibaba.incubator.loadtestermini.model.measurement.report.InteractionResult;

/**
 * MUST support reuse<br/>
 * MUST be thread-safe: no-state<br/>
 * (created at 2010-5-27)
 * 
 * @author <a href="mailto:shuo.qius@alibaba-inc.com">QIU Shuo</a>
 */
public class Session implements InitializingBean {
    private List<Interaction> requestSequence;

    public void setRequestSequence(List<Interaction> requestSequence) {
        this.requestSequence = requestSequence;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (requestSequence == null || requestSequence.size() <= 0)
            throw new IllegalArgumentException("property 'requestSequence' is null or empty~");
    }

    /**
     * execute concurrently
     */
    public void execute(ThinkingTime thinkingTime,
                        SharedResourceHolder sharedResourceHolder,
                        SceneRecorder sceneResultRecorder) throws Exception {
        for (Interaction interaction : requestSequence) {
            thinkingTime.thinking();
            InteractionResult rst = interaction.execute(sharedResourceHolder);
            sceneResultRecorder.addInteractionRestul(rst);
        }
    }

    @Override
    public String toString() {
        //  QS_TODO
        return super.toString();
    }
}
